
#include "protocol.h"


/********************************************************************************************************/
/* 命令接收缓存 */
uint8_t RxBuffer[PTO_MAX_BUF_LEN];
/* 接收数据下标 */
uint8_t RxIndex = 0;
/* 接收状态机 */
uint8_t RxFlag = 0;
/* 新命令接收标志 */
uint8_t New_CMD_flag = 0;
/* 新命令数据长度 */
uint8_t New_CMD_length;
/********************************************************************************************************/
// 获取命令标志
uint8_t Get_CMD_Flag(void)
{
    return New_CMD_flag;
}
// 获取接收的数据
uint8_t *Get_RxBuffer(void)
{
    return (uint8_t *)RxBuffer;
}
// 获取命令长度
uint8_t Get_CMD_Length(void)
{
    return New_CMD_length;
}
// 清除命令数据和相关标志
void Clear_CMD_Flag(void)
{
    for (uint8_t i = 0; i < New_CMD_length; i++)
    {
        RxBuffer[i] = 0;
    }
    New_CMD_length = 0;
    New_CMD_flag = 0;
}
/********************************************************************************************************/
// 接收数据
void Upper_Data_Receive(uint8_t Rx_Temp)
{
    if (New_CMD_flag != 0)
        return;
    switch (RxFlag)
    {
    case 0:
        if (Rx_Temp == PTO_HEAD)
        {
            RxBuffer[0] = PTO_HEAD;
            // printf("PTO_HEAD:%#2x   ", RxBuffer[0]);
            RxFlag = 1;
        }
        else
        {
            RxBuffer[0] = Rx_Temp;
            // printf("PTO_HEAD ERR:%#2x\n", RxBuffer[0]);
            RxBuffer[0] = 0x0;
            RxFlag = 0;
        }
        break;

    case 1:
        if (Rx_Temp == PTO_DEVICE_ID)
        {
            RxBuffer[1] = PTO_DEVICE_ID;
            // printf("PTO_DEVICE_ID:%#2x  ", RxBuffer[1]);
            RxFlag = 2;
            RxIndex = 2;
        }
        else
        {
            RxBuffer[1] = Rx_Temp;
            // printf("PTO_DEVICE_ID ERR:%#2x\n", RxBuffer[1]);
            RxFlag = 0;
            RxBuffer[0] = 0x0;
        }
        break;
    case 2:
        New_CMD_length = Rx_Temp + 2;
        if (New_CMD_length >= PTO_MAX_BUF_LEN)
        {
            // printf("PTO_MAX_BUF_LEN ERR:%#2x\n", New_CMD_length);
            RxIndex = 0;
            RxFlag = 0;
            RxBuffer[0] = 0;
            RxBuffer[1] = 0;
            New_CMD_length = 0;
            break;
        }
        RxBuffer[RxIndex] = Rx_Temp;
        // printf("PTO_MAX_BUF_LEN:%#2x",RxBuffer[RxIndex]);
        RxIndex++;
        RxFlag = 3;
        break;

    case 3:
        RxBuffer[RxIndex] = Rx_Temp;
        // printf("data:%#2x",RxBuffer[RxIndex]);
        RxIndex++;
        if (RxIndex >= New_CMD_length)
        {
            // printf("\n");
            New_CMD_flag = 1;
            RxIndex = 0;
            RxFlag = 0;
        }
        break;

    default:
        break;
    }
}
/********************************************************************************************************/
// 解析数据
void Upper_Data_Parse(uint8_t *data_buf, uint8_t num)
{
    // 包头1 包头2 数量 功能 参数x 校验和
    /* 首先计算校验累加和 */
    int sum = 0;
    for (uint8_t i = 2; i < (num - 1); i++)
        sum += *(data_buf + i);
    sum = sum & 0xFF;
    /* 判断校验累加和 若不同则舍弃*/
    uint8_t recvSum = *(data_buf + num - 1);
    if (!(sum == recvSum))
    {
        printf("Check sum error!, CalSum:%d, recvSum:%d\n", sum, recvSum);
        for (uint8_t i = 0; i < num; i++)
        {
            printf("data_buf[%d]:%#2x    ", i, data_buf[i]);
        }
        printf("\n");
        return;
    }

    uint8_t func_id = *(data_buf + 3);

    switch (func_id)
    {
    /* 判断功能字：蜂鸣器控制 */
    case FUNC_BEEP:
    {
        uint16_t time = *(data_buf + 5) << 8 | *(data_buf + 4);
        // printf("beep:%d\n", time);
        bsp_beep_play(time);
        break;
    }
    default:
        break;
    }
}
